﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using SimpleServiceBus.Bus;
using SimpleServiceBus.Bus.MessageManagement.Callbacks;

namespace SimpleServiceBus.Tests.Bus.MessageManagement.Callbacks
{
    [TestFixture]
    public class DefaultCallbackManagerTestFixture
    {
        [Test]
        public void Can_Start_And_Stop()
        {
            var mgr = new DefaultCallbackManager();
            mgr.Start();
            Assert.IsTrue(mgr.IsRunning);
            mgr.Stop();
            Assert.IsFalse(mgr.IsRunning);
        }

        [Test]
        public void Register_And_Notify()
        {
            bool cbExec = false;

            var mgr = new DefaultCallbackManager();
            mgr.Start();

            string id = Guid.NewGuid().ToString();

            var result = mgr.Register(id).Register((ar) =>
            {
                Assert.That(((CompletionResult)ar.AsyncState).Message, Is.EqualTo("message"));
                Assert.That(((CompletionResult) ar.AsyncState).State, Is.EqualTo("state"));
                cbExec = true;
            }, "state");

            Assert.IsFalse(cbExec);

            mgr.Notify(Guid.NewGuid().ToString(), "message");

            Assert.IsFalse(cbExec);

            mgr.Notify(id, "message");

            Assert.IsTrue(cbExec);

        }
    }
}
